package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.CategoryMapper;
import com.lemeng.dao.ParaMapper;
import com.lemeng.dao.SpecMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Category;
import com.lemeng.service.CategoryService;
import com.lemeng.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CategoryService.class )
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.selectAll();
    }

    @Override
    public PageResult<Category> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categoryMapper.selectAll());
//        Page<Category> categorys = (Page<Category>)categoryMapper.selectAll();
        return new PageResult(categoryPageInfo.getTotal(),categoryPageInfo.getList());
    }

    @Override
    public List<Category> findList(Map<String, Object> serchMap) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("parentId")!=null && StringUtil.isNotEmpty(serchMap.get("parentId").toString())){
                criteria.andEqualTo("parentId",serchMap.get("parentId").toString());
            }
        }
        return categoryMapper.selectByExample(example);
    }

    @Override
    public PageResult<Category> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(categoryPageInfo.getTotal(),categoryPageInfo.getList());
    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Autowired
    SpecMapper specMapper;

    @Autowired
    ParaMapper paraMapper;

    @Override
    @Transactional
    public void delete(Integer id) {
        Category category = new Category();
        category.setParentId(id);
        int i = categoryMapper.selectCount(category);
        if (i>0){
            throw new RuntimeException("当前分类下有子级，不能删除");
        }
        categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map> findCategoryMenus() {
        System.out.println("从redis获取数据");
        return (List<Map>) redisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).get();
    }

    @Autowired
    RedisTemplate redisTemplate;

    public List<Map> findCategoryMenusForRedis() {
        Category category = new Category();
        category.setIsShow("1");
        List<Category> select = categoryMapper.select(category);

        List<Map> menusByParentId = findMenusByParentId(select, 0);
        return menusByParentId;
    }

    @Override
    public void selectCategoryRedis() {
        List<Map> categoryMenus = findCategoryMenusForRedis();
        redisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).set(categoryMenus);

    }


    public List<Map> findMenusByParentId(List<Category> categoryList,Integer pid){
        ArrayList<Map> list = new ArrayList<>();

        for (Category category : categoryList) {
            if (category.getParentId().equals(pid)) {
                HashMap map = new HashMap();
                map.put("name",category.getName());
                List<Map> menusByParentId = findMenusByParentId(categoryList, category.getId());
                if (!CollectionUtils.isEmpty(menusByParentId)){
                    map.put("menus",menusByParentId);
                }
                list.add(map);
            }
        }
        return list;
    }
}
